package kr.co.happenstance.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;

import kr.co.happenstance.CustomAlertDialogBuilder;
import kr.co.happenstance.ProfileActivity;
import kr.co.happenstance.R;
import kr.co.happenstance.lib.vo.RequestInfo;
import kr.co.happenstance.util.Util;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class RequestAdapter extends BaseAdapter {
	protected final String TAG = "<<<RequestAdapter>>>";
	
	private SimpleDateFormat formatter = null;
	
	private Context context;
	private LayoutInflater inflater = null;
	private ArrayList<RequestInfo> requestList;
	private ViewHolder viewHolder = null;
	
	private final View.OnClickListener profileOnClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			int position = (Integer) v.getTag();
			Log.d(TAG, "position["+position+"]");
			Log.d(TAG, "profileYn["+requestList.get(position).profileYn+"]");
			
			if(requestList.get(position).profileYn.equals("Y") ){
				Intent intent = new Intent(context, ProfileActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				intent.putExtra("profileId", requestList.get(position).requestId);
				context.startActivity(intent);
			}else{
				new CustomAlertDialogBuilder(context, R.string.alert_title, R.string.alert_msg_profile).create();
			}
		}
		
	};	
	
	public RequestAdapter(Context context, ArrayList<RequestInfo> _list){
		this.context = context;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.requestList = _list;
		formatter = new SimpleDateFormat("a hh:mm", Locale.ENGLISH);
	}	
	
	@Override
	public int getCount() {
		return requestList.size();
	}

	@Override
	public Object getItem(int position) {
		return requestList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.d(TAG, "position["+position+"]");
		View v = convertView;
		try{
			if(v == null){
				 v = inflater.inflate(R.layout.adapter_request, parent, false);
				 viewHolder = new ViewHolder();
				 viewHolder.btnListProfile = (ImageButton) v.findViewById(R.id.btn_list_profile);
				 viewHolder.tvListDistance = (TextView) v.findViewById(R.id.tv_list_distance);
				 viewHolder.tvListPromiseDate = (TextView) v.findViewById(R.id.tv_list_promise_date);
				 v.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder)v.getTag();
			}
			
			viewHolder.btnListProfile.setTag(position);
			viewHolder.btnListProfile.setOnClickListener(profileOnClickListener);
			
			viewHolder.tvListDistance.setText(Double.toString(requestList.get(position).distance) + "km");
			viewHolder.tvListPromiseDate.setText(formatter.format(Util.getCalendar(requestList.get(position).promiseDate).getTime()));
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return v;
	}
	
	private class ViewHolder{
		public ImageButton btnListProfile;
		public TextView tvListDistance;
		public TextView tvListPromiseDate;
	}
}
